<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">车间</label>
        <el-input v-model="query.facility" clearable placeholder="车间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产线</label>
        <el-input v-model="query.wipLine" clearable placeholder="产线" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产线名称</label>
        <el-input v-model="query.wipLineName" clearable placeholder="产线名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-input v-model="query.status" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="车间" prop="facility">
            <el-select v-model="form.facility" filterable placeholder="请选择">
              <el-option
                v-for="item in facilitys.content"
                :key="item.id"
                :label="item.facilityname"
                :value="item.facility"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产线" prop="wipLine">
            <el-input v-model="form.wipLine" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产线名称" prop="wipLineName">
            <el-input v-model="form.wipLineName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio v-for="item in dict.wipline_status" :key="item.id" v-model="form.status" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="facility" label="车间" />
        <el-table-column prop="wipLine" label="产线" />
        <el-table-column prop="wipLineName" label="产线名称" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :disabled="wipline.id===scope.row.id"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              :active-value="1"
              :inactive-value="0"
              @change="changeEnabled(scope.row, scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','wipline:edit','wipline:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudWipline from '@/api/system/wipline'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import facility, { getFacilitys } from '@/api/system/facility'
import wipline from '@/api/system/wipline'

const defaultForm = { id: null, facility: null, wipLine: null, wipLineName: null, status: null }
export default {
  name: 'Wipline',
  components: { pagination, crudOperation, rrOperation, udOperation },
  computed: {
    wipline() {
      return wipline
    },
    facility() {
      return facility
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['wipline_status'],
  cruds() {
    return CRUD({ title: '系统：产线', url: 'api/wipline', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWipline }})
  },
  data() {
    return {
      facilitys: [],
      permission: {
        add: ['admin', 'wipline:add'],
        edit: ['admin', 'wipline:edit'],
        del: ['admin', 'wipline:del']
      },
      rules: {
        facility: [
          { required: true, message: '车间不能为空', trigger: 'blur' }
        ],
        wipLine: [
          { required: true, message: '产线不能为空', trigger: 'blur' }
        ],
        wipLineName: [
          { required: true, message: '产线名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'facility', display_name: '车间' },
        { key: 'wipLine', display_name: '产线' },
        { key: 'wipLineName', display_name: '产线名称' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeToCU](crud, form) {
      this.getFacility()
      this.handleData(form)
    },
    getFacility() {
      getFacilitys({ enabled: true }).then(res => {
        this.facilitys = res
      }).catch(() => {})
    },
    handleData(form) {
      this.form.status = '1'
      return form
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.wipline_status[val] + '" ' + data.facility + '车间, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudWipline.edit(data).then(res => {
          this.crud.notify(this.dict.label.wipline_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(err => {
          if (data.status === 0) {
            data.status = 1
          } else {
            data.status = 0
          }
          console.log(err.response.data.message)
        })
      }).catch(() => {
        if (data.status === 0) {
          data.status = 1
        } else {
          data.status = 0
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
